using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace TestWidget
{
    public partial class Default2 : System.Web.UI.Page
    {

        protected override void OnPreLoad(EventArgs e)
        {
            base.OnPreLoad(e);
            System.Web.UI.Control layout;
            if (Request.QueryString["id"] != null)
            {
                string path = Request.QueryString["id"];
                layout = LoadControl(path);
            }
            else
            {
                layout = LoadControl(drgWidgetLayout.SelectedValue);
            }

            layout.ID = "LayoutPanelTest";
            LayoutPanel.Controls.Add(layout);

        }
        //protected override void OnInit(EventArgs e)
        //{
        //    base.OnInit(e);
        //    System.Web.UI.Control layout;
        //    if (Request.QueryString["id"] != null)
        //    {
        //        string path = Request.QueryString["id"];
        //        layout = LoadControl(path);
        //    }
        //    else
        //    {
        //        layout = LoadControl(drgWidgetLayout.SelectedValue);
        //    }

        //    layout.ID = "LayoutPanelTest";
        //    LayoutPanel.Controls.Add(layout);
        //}

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {           
            }
            SetupPage();
        }

        protected void SetupPage()
        {
            //Panel[] columnPanel = new Panel[]{
            //                            this.WidgetPanelsLayout.FindControl("PanelLeft") as Panel,
            //                            this.WidgetPanelsLayout.FindControl("PanelMiddle") as Panel,
            //                            this.WidgetPanelsLayout.FindControl("PanelRight") as Panel
            //                        };
            PlaceHolder[] columnPanel = new PlaceHolder[]{
                                        this.FindControl("LayoutPanelTest").FindControl("PanelLeft") as PlaceHolder,
                                        this.FindControl("LayoutPanelTest").FindControl("PanelMiddle") as PlaceHolder,
                                        this.FindControl("LayoutPanelTest").FindControl("PanelRight") as PlaceHolder
                                    };

            foreach (PlaceHolder pn in columnPanel)
            {
                if (pn != null)
                {
                    foreach (System.Web.UI.Control ct in pn.Controls)
                    {
                        WidgetContainer container = ct as WidgetContainer;
                        if (container != null)
                            ct.Controls.Remove(container);
                    }
                }
            }
            // add control to page
            IList list = Data.GetAllWidgetInstances();
            foreach(WidgetInstance wd in list)
            {
                PlaceHolder panel;
                int column = wd.Column;
                do 
                {
                    panel = columnPanel[column];
                    column--;
                }while(panel == null);

                WidgetContainer container = LoadControl("WidgetContainer.ascx") as WidgetContainer;
                container.WidgetInstance = wd;
                container.ID = "WidgetContainer" + wd.Id.ToString();
                try
                {
                    panel.Controls.Add(container);
                }
                catch { }
            }
        }

        protected void drgWidgetLayout_SelectedIndexChanged(object sender, EventArgs e)
        {

        }       
    }
}
